package com.ya;

import java.util.*;

public class Room {
    // 需要一个集合装做好的牌
    private static List<Card> allCards = new ArrayList<>();
    // 因为每个房间只有一副牌，所有需要对牌初始化，静态遍历的初始化
    // 利用代码块，静态代码块顺序大于对象的代码块
    static {
        String[] colors = {"♡", "♢", "♤", "♧"};
        String[] points = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        int count = 0;
        for (String point : points) {
            ++count;
            for (String color : colors) {
                allCards.add(new Card(point,color,count));
            }
        }
        // 加入大小王
        allCards.addAll(Arrays.asList(new Card("","☣",++count),new Card("","☢",count)));
        System.out.println("洗牌前："+allCards);
    }

    public void start() {
        // 洗牌
        Collections.shuffle(allCards);
        System.out.println("洗牌后："+allCards);

        //发牌
        // 要给桑人发牌，要留三张底牌
        // 一人有17张，需要使用map的数据结果
        Map<String,List<Card>> RoomMap = new HashMap();
        List<Card> w1List = new ArrayList<>();
        RoomMap.put("w1",w1List);

        List<Card>c2List = new ArrayList<>();
        RoomMap.put("c2",c2List);

        List<Card> y3List = new ArrayList<>();
        RoomMap.put("y3",y3List);

        for (int i = 1; i <= 51; i++) {
            if (i%3 == 0) {
                w1List.add(allCards.get(i-1));
            } else if (i%3 == 1) {
                c2List.add(allCards.get(i-1));
            } else {
                y3List.add(allCards.get(i-1));
            }
        }

        // 抢地主
        List<Card> subList = allCards.subList(51,allCards.size());
        String[] people = {"w1","c2","y3"};
        int index = (int) (Math.random()*people.length);
        RoomMap.get(people[index]).addAll(subList);

        System.out.println("w1的牌：" + RoomMap.get("w1"));
        System.out.println("c2的牌：" + RoomMap.get("c2"));
        System.out.println("y3的牌：" + RoomMap.get("y3"));
        System.out.println("底牌是："+subList);

        // 对牌排序
        w1List.sort(Comparator.comparingInt(Card::getValue));
        c2List.sort(Comparator.comparingInt(Card::getValue));
        y3List.sort(Comparator.comparingInt(Card::getValue));

        // 洗牌后
        System.out.println("洗牌后，w1的牌：" + RoomMap.get("w1"));
        System.out.println("洗牌后，c2的牌：" + RoomMap.get("c2"));
        System.out.println("洗牌后，y3的牌：" + RoomMap.get("y3"));

    }
}
